package com.shankungfu.adapter;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.shankungfu.MainApplication;
import com.shankungfu.R;
import com.shankungfu.bean.CommentListDataBean;
import com.shankungfu.bean.ReplyBean;
import com.shankungfu.main.CommentActivity;
import com.shankungfu.main.LogInActivity;
import com.shankungfu.main.SecondCommentActivity;
import com.shankungfu.util.PhoneUtils;
import com.shankungfu.util.ToastUtil;
import com.shankungfu.view.HorizontalListView;

public class CommentReplyAdapter extends BaseAdapter {

	private Context context;
	private List<CommentListDataBean> commentList;
	private DisplayImageOptions options;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	private ImageLoader imageLoader;

	public CommentReplyAdapter(Context context, DisplayImageOptions options) {
		this.context = context;
		this.options = options;
		imageLoader = ImageLoader.getInstance();
	}
	
	public void setData(List<CommentListDataBean> commentList) {
		this.commentList = commentList;
	}

	@Override
	public int getCount() {
		return commentList != null ? commentList.size() : 0;
	}

	@Override
	public CommentListDataBean getItem(int position) {
		return commentList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.message_child_item_layout,
					null);
			holder.author = (TextView) convertView.findViewById(R.id.message_child_author);
			holder.date = (TextView) convertView.findViewById(R.id.message_child_date);
			holder.content = (TextView) convertView.findViewById(R.id.message_child_content);
			holder.user_icon = (ImageView) convertView.findViewById(R.id.message_child_user_pic);
			holder.pic_listview = (HorizontalListView) convertView
					.findViewById(R.id.pic_listview);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final CommentListDataBean commentListDataBean = commentList.get(position);
		
		holder.author.setText(commentListDataBean.getUser_name());
		holder.date.setText(PhoneUtils.TimeStamp2Date(commentListDataBean.getAdd_time(), "yyyy年MM月DD日  HH时mm分"));
		holder.content.setText(Html.fromHtml(commentListDataBean.getComment_content()));
		holder.pic_listview.setVisibility(View.GONE);
		
		imageLoader.displayImage(commentListDataBean.getUser_img_url(), holder.user_icon, options, animateFirstListener);
		
//		convertView.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				if (MainApplication.isUserIsLogin()) {
//					Intent intent = new Intent(context, SecondCommentActivity.class);
//					Bundle bundle = new Bundle();
//					bundle.putString("reply_id", commentListDataBean.getReply_id());
//					bundle.putString("author", commentListDataBean.getUser_name());
//					bundle.putString("date", commentListDataBean.getAdd_time());
//					bundle.putString("content", commentListDataBean.getComment_content());
//					bundle.putString("image", commentListDataBean.getUser_img_url());
//					intent.putExtra("replyBean", bundle);
//					context.startActivity(intent);
//				} else {
//					ToastUtil.showMessageText(context, "请先登录");
//					Intent intent = new Intent(context, LogInActivity.class);
//					((Activity) context).startActivityForResult(intent, CommentActivity.DENGLU_SUCCESS_CODE);
//				}
//				
//			}
//		});

		return convertView;
	}

	private class ViewHolder {
		private TextView author;
		private TextView date;
		private TextView content;
		private ImageView user_icon;
		private HorizontalListView pic_listview;
	}
	
	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}

}
